'use strict';

angular.module('myApp.listcapview', ['ngRoute', 'ngGrid', 'ui.bootstrap'])

        .config(['$routeProvider', function ($routeProvider) {
                $routeProvider.when('/listcapview', {
                    templateUrl: 'listcapview/listcapview.html',
                    controller: 'listcapviewCtrl'
                });
            }])

        .controller('listcapviewCtrl', ['$scope', '$modal', '$http', function ($scope, $modal, $http) {



                $scope.mySelections = [];

                $scope.filterOptions = {
                    filterText: "",
                    useExternalFilter: true
                };
                $scope.totalServerItems = 0;
                $scope.pagingOptions = {
                    pageSizes: [400, 800, 1000],
                    pageSize: 400,
                    currentPage: 1
                };
                $scope.setPagingData = function (data, page, pageSize) {
                    var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
                    $scope.myData = pagedData;
                    $scope.totalServerItems = data.length;
                    if (!$scope.$$phase) {
                        $scope.$apply();
                    }
                };
                $scope.getPagedDataAsync = function (pageSize, page, searchText) {
                    setTimeout(function () {
                        var data;
                        if (searchText) {
                            var ft = searchText.toLowerCase();
                            $http.get('file/cap1-400.json').success(function (largeLoad) {
                                data = largeLoad.filter(function (item) {
                                    return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                                });
                                $scope.setPagingData(data, page, pageSize);
                            });
                        } else {
                            $http.get('file/cap1-400.json').success(function (largeLoad) {
                                $scope.setPagingData(largeLoad, page, pageSize);
                            });
                        }
                    }, 100);
                };

                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

                $scope.$watch('pagingOptions', function (newVal, oldVal) {
                    if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
                    }
                }, true);
                $scope.$watch('filterOptions', function (newVal, oldVal) {
                    if (newVal !== oldVal) {
                        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
                    }
                }, true);

                $scope.gridOptions = {
                    data: 'myData',
                    enableColumnResize: true,
                    columnDefs: [{field: 'id', displayName: 'id', width: "10%"},
                        {field: 'cmecc', displayName: 'Cod mecc.', width: "10%"},
                        {field: 'cap', displayName: 'Capitolo', width: "10%"},
                        {field: 'art', displayName: 'Articolo', width: "10%"},
                        {field: 'sglcresp', displayName: 'Centro resp.', width: "10%"},
                        {field: 'desc', displayName: 'Descrizione', width: "80%"},
                        {field: 'respres', displayName: 'Residui presunti', width: "10%"},
                        {field: 'prevcomp', displayName: 'Previsione di competenza', width: "10%"},
                        {field: 'prevcassa', displayName: 'Previsione di cassa', width: "10%"},
                        {field: 'dispcomp', displayName: 'Disp. competenza', width: "10%"},
                        {field: 'dispcassa', displayName: 'Disp. cassa', width: "10%"},
                        {field: 'prevcobipro', displayName: 'Prev. comp. bil. prov', width: "10%"},
                        {field: 'varcomp', displayName: 'Var. comp.', width: "10%"},
                        {field: 'varcassa', displayName: 'Var. cassa', width: "10%"}
                    ],
                    multiSelect: false,
                    enablePaging: true,
//		        footerTemplate:"<div ng-grid-footer=\"\" class=\"ng-scope\"><div ng-show=\"showFooter\" class=\"ngFooterPanel ng-scope\" ng-class=\"{'ui-widget-content': jqueryUITheme, 'ui-corner-bottom': jqueryUITheme}\" ng-style=\"footerStyle()\" style=\"width: 600px; height: 55px;\">"+
//
//		                "<div class=\"ngPagerContainer ngNoMultiSelect\" style=\"float: right; margin-top: 10px;\" ng-show=\"enablePaging\" ng-class=\"{'ngNoMultiSelect': !multiSelect}\">"+
//
//		                " <div style=\"float:left; margin-right: 10px; line-height:25px;\" class=\"ngPagerControl\">"+
//		                "<button type=\"button\" class=\"ngPagerButton\" ng-click=\"pageBackward()\" ng-disabled=\"cantPageBackward()\" title=\"Previous Page\"><div class=\"ngPagerFirstTriangle ngPagerPrevTriangle\"></div></button>"+
//		                " <button type=\"button\" class=\"ngPagerButton\" ng-click=\"pageForward()\" ng-disabled=\"cantPageForward()\" title=\"Next Page\" disabled=\"disabled\"><div class=\"ngPagerLastTriangle ngPagerNextTriangle\"></div></button>"+
//		                "  </div>"+
//		                "</div>"+
//		                "</div></div>",
//		            
//		        
//		        



                    showFooter: true,
                    totalServerItems: 'totalServerItems',
                    pagingOptions: $scope.pagingOptions,
                    filterOptions: $scope.filterOptions,
                    selectedItems: $scope.mySelections,
                    rowTemplate: '<div ng-dblclick="onDblClickRow(row)" ng-style="{\'cursor\': row.cursor, \'z-index\': col.zIndex() }" ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngCell {{col.cellClass}}" ng-cell></div>'



                };

                $scope.onDblClickRow = function (r) {
                    console.log(r.entity);

                    //
                    var modalInstance = $modal.open({
                        templateUrl: 'listcapview/detail.html',
                        controller: 'detailCtrl',
                        size: 'lg',
                        resolve: {
                            modalModel: function () {
                                return {
                                    id: r.entity.id,
                                    cmecc: r.entity.cmecc,
                                    cap: r.entity.cap,
                                    art: r.entity.art,
                                    sglcresp: r.entity.sglcresp,
                                    desc: r.entity.desc,
                                    respres: r.entity.respres,
                                    prevcomp:r.entity.prevcomp,
                                    prevcassa: r.entity.prevcassa,
                                    dispcomp: r.entity.dispcomp,
                                    dispcassa: r.entity.dispcassa,
                                    prevcobipro: r.entity.prevcobipro,
                                    prevcabipro: r.entity.prevcabipro


                                }
                            }
                        }
                    });
                    //

                };





            }]);